#include "utils.h"

namespace agr_utils{
	bool getIP(IPv4 & myIP)
	{
		char szBuffer[1024];

		#ifdef WIN32
		WSADATA wsaData;
		WORD wVersionRequested = MAKEWORD(2, 0);
		if(::WSAStartup(wVersionRequested, &wsaData) != 0)
			return false;
		#endif


		if(gethostname(szBuffer, sizeof(szBuffer)) == SOCKET_ERROR)
		{
			#ifdef WIN32
			WSACleanup();
			#endif
			return false;
		}

		struct hostent *host = gethostbyname(szBuffer);
		if(host == NULL)
		{
			#ifdef WIN32
			WSACleanup();
			#endif
			return false;
		}

		//Obtain the computer's IP
		myIP.b1 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b1;
		myIP.b2 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b2;
		myIP.b3 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b3;
		myIP.b4 = ((struct in_addr *)(host->h_addr))->S_un.S_un_b.s_b4;

		#ifdef WIN32
		WSACleanup();
		#endif
		return true;
	}

	char *IPv4toc(IPv4 ip){
		char *cIP = new char[16];
		sprintf(cIP, "%d.%d.%d.%d", ip.b1, ip.b2, ip.b3, ip.b4);

		return cIP;
	}

	char *getName(char *path){
		int len = strlen(path);

		for(int i = len-1; i > 0; i --){
			if(path[i] == '\\'){
				char *name = new char[len - i];
				strcpy(name, &(path[i+1]));

				return name;
			}
		}

		return NULL;
	}
}